package com.sdtele.tta.servlets;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.Doc;

@WebServlet("/front/DownloadDoc")
@MultipartConfig
public class DownloadDoc extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 request.getRequestDispatcher("/WEB-INF/UploadDocTest.jsp").forward(request, response);

		 //	doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// final Logger log = Logger.getLogger(DownloadDoc.class);
		//	long docId = Long.parseLong(request.getParameter("docId"));
		 long docId =2096;
		Doc doc = Dac.getInstance().getDoc(docId);
		String filePath = doc.getRealPath();
		String fileName = doc.getName();
		downloadFile(filePath, fileName, response);
	}

	private static void downloadFile(String filePath, String fileName,

			HttpServletResponse response) {

		File file = new File(filePath);

		if (!file.exists() || file.isDirectory()) {

			output(response, "您请求的文件不存在或者路径有误！");

			return;

		}

		try {

			// 对下载的文件名称进行编码，避免出现中文乱码问题

			response.setHeader("Content-disposition", "attachment; filename="

					+ new String(fileName.getBytes("GBK"), "ISO-8859-1"));

			InputStream is = new FileInputStream(file);
			BufferedInputStream	 bufferedInputStream = new BufferedInputStream( is ) ;
			

			

			ServletOutputStream out = response.getOutputStream();
			
			byte[] b=new byte[1048576]; 
			 int length = 0 ;
			 while( (length = bufferedInputStream.read( b ) )!= -1 ){
	                //length 代表实际读取的字节数
	                out.write(b, 0, length );
	            }
            
		

			is.close();

			out.flush();

			out.close();

		} catch (FileNotFoundException notFound) {

			output(response, "您所请求的文件不存在！");

		} catch (IOException ioe) {

			ioe.printStackTrace();

			output(response, "您所请求的文件出现异常！");

		} catch (Exception e) {

			e.printStackTrace();

			output(response, "您所请求的文件出现异常！");

		}

	}

	private static void output(HttpServletResponse response, String message) {

		try {

			response.setContentType("text/html; charset=UTF-8");

			Writer out = response.getWriter();

			out.write(message);

			out.flush();

			out.close();

		} catch (Exception e) {

			e.printStackTrace();

		}

	}

}
